package com.vhall.component.plugin.common.config;

import com.vhall.component.plugin.common.filter.RequestLogFilter;
import com.vhall.component.plugin.common.properties.ReqLogProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

import static com.vhall.component.framework.common.constant.FilterOrder.REQUEST_LOG_ORDER;

/**
 * 请求日志 自动配置, 当为web时候自动生效
 *
 * @author Administrator
 */
@Configuration
@AutoConfigureAfter(WebAutoConfigure.class)
public class RequestLogAutoConfigure {

    /**
     * 创建 RequestLogFilter Bean，记录 API 请求日志
     */
    @Bean
    public FilterRegistrationBean<RequestLogFilter> reqLogFilter(ReqLogProperties properties) {
        RequestLogFilter filter = new RequestLogFilter(properties);
        return createFilterBean(filter, REQUEST_LOG_ORDER);
    }

    private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
        FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
        bean.setOrder(order);
        return bean;
    }

}
